package com.actions.bluetoothbox2.util;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.actions.bluetoothbox2.R;
import com.actions.ibluz.manager.BluzManager;

/**
 * Created by liuyong on 2018/1/4.
 */

public class SelecterDailog extends Dialog implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private LinearLayout ll_nosound, ll_vbass, ll_treble, ll_peq, ll_mdrc, ll_enhancement, ll_weaken;
    private CheckBox cb_nosound, cb_vbass, cb_treble, cb_peq, cb_mdrc, cb_enhancement, cb_weaken;
    private Button btn_ok;
    private boolean mDaeChoose[];
    private Context context;
    private BluzManager mBluzManager;

    public SelecterDailog(@NonNull Context context, boolean mDaeChoose[], BluzManager mBluzManager) {
        super(context);
        this.context = context;
        this.mDaeChoose = mDaeChoose;
        this.mBluzManager = mBluzManager;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_select_layout);
        ll_nosound = (LinearLayout) findViewById(R.id.ll_nosound);
        ll_vbass = (LinearLayout) findViewById(R.id.ll_vbass);
        ll_treble = (LinearLayout) findViewById(R.id.ll_treble);
        ll_peq = (LinearLayout) findViewById(R.id.ll_peq);
        ll_mdrc = (LinearLayout) findViewById(R.id.ll_mdrc);
        ll_enhancement = (LinearLayout) findViewById(R.id.ll_enhancement);
        ll_weaken = (LinearLayout) findViewById(R.id.ll_weaken);
        btn_ok = (Button) findViewById(R.id.btn_ok);

        cb_nosound = (CheckBox) findViewById(R.id.cb_nosound);
        cb_vbass = (CheckBox) findViewById(R.id.cb_vbass);
        cb_treble = (CheckBox) findViewById(R.id.cb_treble);
        cb_peq = (CheckBox) findViewById(R.id.cb_peq);
        cb_mdrc = (CheckBox) findViewById(R.id.cb_mdrc);
        cb_enhancement = (CheckBox) findViewById(R.id.cb_enhancement);
        cb_weaken = (CheckBox) findViewById(R.id.cb_weaken);

        cb_vbass.setChecked(mDaeChoose[0]);
        cb_treble.setChecked(mDaeChoose[1]);
        cb_peq.setChecked(mDaeChoose[4]);
        cb_mdrc.setChecked(mDaeChoose[5]);
        cb_enhancement.setChecked(mDaeChoose[6]);
        cb_weaken.setChecked(mDaeChoose[7]);
        cb_nosound.setOnCheckedChangeListener(this);
        cb_vbass.setOnCheckedChangeListener(this);
        cb_treble.setOnCheckedChangeListener(this);
        cb_peq.setOnCheckedChangeListener(this);
        cb_mdrc.setOnCheckedChangeListener(this);
        cb_enhancement.setOnCheckedChangeListener(this);
        cb_weaken.setOnCheckedChangeListener(this);

        ll_nosound.setOnClickListener(this);
        ll_vbass.setOnClickListener(this);
        ll_treble.setOnClickListener(this);
        ll_peq.setOnClickListener(this);
        ll_mdrc.setOnClickListener(this);
        ll_enhancement.setOnClickListener(this);
        ll_weaken.setOnClickListener(this);
        btn_ok.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_nosound:
                cb_nosound.setChecked(!cb_nosound.isChecked());
                break;
            case R.id.ll_vbass:
                if (!cb_nosound.isChecked()) {
                    cb_vbass.setChecked(!cb_vbass.isChecked());
                }
                break;
            case R.id.ll_treble:
                if (!cb_nosound.isChecked()) {
                    cb_treble.setChecked(!cb_treble.isChecked());
                }
                break;
            case R.id.ll_peq:
                if (!cb_nosound.isChecked()) {
                    cb_peq.setChecked(!cb_peq.isChecked());
                }
                break;
            case R.id.ll_mdrc:
                if (!cb_nosound.isChecked()) {
                    cb_mdrc.setChecked(!cb_mdrc.isChecked());
                }
                break;
            case R.id.ll_enhancement:
                if (!cb_nosound.isChecked()) {
                    cb_enhancement.setChecked(!cb_enhancement.isChecked());
                }
                break;
            case R.id.ll_weaken:
                if (!cb_nosound.isChecked()) {
                    cb_weaken.setChecked(!cb_weaken.isChecked());
                }
                break;
            case R.id.btn_ok:
                if(cb_nosound.isChecked()){
                    mBluzManager.setDAENoDigitalSound();
                }else {
                    mDaeChoose[2] = false;
                    mDaeChoose[3] = false;
                    int option = Utils.LittleEndian.BitToByte(mDaeChoose);
                    Preferences.setPreferences(context, Preferences.KEY_DAE_OPTION, option);
                    mBluzManager.setDAEOption((byte) option);
                }
                cancel();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_nosound:
                if (isChecked) {
                    cb_vbass.setChecked(false);
                    cb_treble.setChecked(false);
                    cb_peq.setChecked(false);
                    cb_mdrc.setChecked(false);
                    cb_enhancement.setChecked(false);
                    cb_weaken.setChecked(false);

                    cb_vbass.setEnabled(false);
                    cb_treble.setEnabled(false);
                    cb_peq.setEnabled(false);
                    cb_mdrc.setEnabled(false);
                    cb_enhancement.setEnabled(false);
                    cb_weaken.setEnabled(false);
                } else {
                    cb_vbass.setEnabled(true);
                    cb_treble.setEnabled(true);
                    cb_peq.setEnabled(true);
                    cb_mdrc.setEnabled(true);
                    cb_enhancement.setEnabled(true);
                    cb_weaken.setEnabled(true);
                }
                break;
            case R.id.cb_vbass:
                mDaeChoose[0] = isChecked;
                break;
            case R.id.cb_treble:
                mDaeChoose[1] = isChecked;
                break;
            case R.id.cb_peq:
                mDaeChoose[4] = isChecked;
                break;
            case R.id.cb_mdrc:
                mDaeChoose[5] = isChecked;
                break;
            case R.id.cb_enhancement:
                mDaeChoose[6] = isChecked;
                break;
            case R.id.cb_weaken:
                mDaeChoose[7] = isChecked;
                break;
            default:
                mDaeChoose = new boolean[]{false, false, false, false, false, false, false, false};
                break;
        }
    }
}
